package com.wr.vehicle.service.impl;

import com.wr.common.utils.DateUtils;
import com.wr.common.utils.StringUtils;
import com.wr.common.utils.ZxingUtil;
import com.wr.common.utils.uuid.UUID;
import com.wr.vehicle.domain.vo.DetailsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wr.vehicle.mapper.EbikeMapper;
import com.wr.vehicle.domain.Ebike;
import com.wr.vehicle.service.IEbikeService;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 车辆信息Service业务层处理
 *
 * @author art8173
 * @date 2024-11-25
 */
@Service
public class EbikeServiceImpl implements IEbikeService {
    @Autowired
    private EbikeMapper ebikeMapper;

    /**
     * 导出二维码
     *
     * @param params 前端上传的参数，键值对 licensePlate: qrId
     * @return 二维码保存的目录路径
     */
    @Override
    public byte[] exportQrCodes(Map<String, String> params) throws IOException {
        String tempDirPath = System.getProperty("java.io.tmpdir") + File.separator + "qr_codes";
        File tempDir = new File(tempDirPath);
        if (!tempDir.exists()) {
            tempDir.mkdirs();
        }
        for (Map.Entry<String, String> entry : params.entrySet()) {
            String licensePlate = entry.getKey();
            String qrId = entry.getValue();
            try {
                // 生成二维码图片
                BufferedImage qrCodeImage = ZxingUtil.createQRCode(qrId);
                // 保存二维码图片到临时目录
                String filePath = tempDirPath + File.separator + licensePlate + ".png";
                ZxingUtil.saveImageToFile(qrCodeImage, filePath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // 将临时目录中的图片压缩成ZIP文件
        byte[] zipBytes = zipDirectory(tempDirPath);
        // 删除临时目录及其内容
        deleteDir(tempDirPath);
        return zipBytes;
    }

    private byte[] zipDirectory(String dirPath) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream);
        File dir = new File(dirPath);

        if (dir.exists() && dir.isDirectory()) {
            File[] files = dir.listFiles();
            if (files != null) {
                for (File file : files) {
                    try (FileInputStream fis = new FileInputStream(file);
                         BufferedInputStream bis = new BufferedInputStream(fis)) {
                        ZipEntry zipEntry = new ZipEntry(file.getName());
                        zipOutputStream.putNextEntry(zipEntry);
                        byte[] buffer = new byte[1024];
                        int bytesRead;
                        while ((bytesRead = bis.read(buffer)) != -1) {
                            zipOutputStream.write(buffer, 0, bytesRead);
                        }
                        zipOutputStream.closeEntry();
                    }
                }
            }
        }

        zipOutputStream.close();
        return byteArrayOutputStream.toByteArray();
    }

    private void deleteDir(String dirPath) {
        File dir = new File(dirPath);
        if (dir.exists() && dir.isDirectory()) {
            File[] files = dir.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isFile()) {
                        file.delete();
                    }
                }
            }
            dir.delete();
        }
    }





    /**
     * 查询车辆信息
     *
     * @param ebikeId 车辆信息主键
     * @return 车辆信息
     */
    @Override
    public Ebike selectEbikeByEbikeId(Long ebikeId) {
        return ebikeMapper.selectEbikeByEbikeId(ebikeId);
    }

    /**
     * 根据 qr_id 查询 license_plate
     *
     * @param qrId 二维码ID
     * @return license_plate
     */
    @Override
    public String selectLicensePlateByQrId(String qrId) {
        return ebikeMapper.selectLicensePlateByQrId(qrId);
    }

    /**
     * 查询车辆与违规信息
     *
     * @param ebikeId 车辆信息主键
     * @return 车辆信息
     */
    @Override
    public DetailsVo selectDetailsByEbikeId(Long ebikeId) {
        return ebikeMapper.selectDetailsByEbikeId(ebikeId);
    }

    /**
     * 查询车辆信息列表
     *
     * @param ebike 车辆信息
     * @return 车辆信息
     */
    @Override
    public List<Ebike> selectEbikeList(Ebike ebike) {
        return ebikeMapper.selectEbikeList(ebike);
    }

    /**
     * 新增车辆信息
     *
     * @param ebike 车辆信息
     * @return 结果
     */
    @Override
    public int insertEbike(Ebike ebike) {
        ebike.setCreateTime(DateUtils.getNowDate());
        // 生成 UUID 并设置 qrId
        ebike.setQrId(UUID.randomNumericUUID());
        return ebikeMapper.insertEbike(ebike);
    }

    /**
     * 修改车辆信息
     *
     * @param ebike 车辆信息
     * @return 结果
     */
    @Override
    public int updateEbike(Ebike ebike) {
        ebike.setUpdateTime(DateUtils.getNowDate());
        return ebikeMapper.updateEbike(ebike);
    }

    /**
     * 批量删除车辆信息
     *
     * @param ebikeIds 需要删除的车辆信息主键
     * @return 结果
     */
    @Override
    public int deleteEbikeByEbikeIds(Long[] ebikeIds) {
        return ebikeMapper.deleteEbikeByEbikeIds(ebikeIds);
    }

    /**
     * 删除车辆信息信息
     *
     * @param ebikeId 车辆信息主键
     * @return 结果
     */
    @Override
    public int deleteEbikeByEbikeId(Long ebikeId) {
        return ebikeMapper.deleteEbikeByEbikeId(ebikeId);
    }
}
